//修改用户信息
import { Toast } from 'antd-mobile'
const errToast = (msg) => {
    Toast.info(`${msg}`, 1)
}

const modifybus = (business_id, business_name, business_address, File, props) => {
    const formData = new FormData()
    formData.append('business_id', business_id)
    formData.append('business_name', business_name)
    formData.append('business_address', business_address)
    console.log(business_address)
    if (File[0].file == undefined) {
        formData.append('File', File[0].url)
    } else {
        formData.append('File', File[0].file)
    }
    return (dispatch) => {
        // console.log(formData)
        let url = 'http://www.h5weixin.club:1024/api/business/'
        fetch(url, {
            method: 'PUT',
            mode: 'cors',
            body: formData
        }).then((res) => {
            // console.log(res)
            return res.json()
        }).then((res) => {
            errToast(res.msg)
            console.log(res)
            localStorage.setItem('user', JSON.stringify(res.data[0]))
            dispatch({
                type: 'ADD_UR',
                value: res.data[0]
            })
            props.history.goBack()
        }).catch((err) => {
            console.log(err);
        })

    }
}

const modifypaybus = (business_id, business_password,File1, props) => {
    const formData = new FormData()
    formData.append('business_id', business_id)
    formData.append('password',business_password)
    if (File1[0].file == undefined) {
        formData.append('File1', File1[0].url)
    } else {
        formData.append('File1', File1[0].file)
    }
    return (dispatch) => {
        // console.log(formData)
        let url = 'http://www.h5weixin.club:1024/api/business/2'
        fetch(url, {
            method: 'PUT',
            mode: 'cors',
            body: formData
        }).then((res) => {
            // console.log(res)
            return res.json()
        }).then((res) => {
            errToast(res.msg)
            console.log(res)
            localStorage.setItem('user', JSON.stringify(res.data[0]))
            dispatch({
                type: 'ADD_UR',
                value: res.data[0]
            })
            props.history.goBack()
        }).catch((err) => {
            console.log(err);
        })

    }
}

const modifybussoft = (business_id, business_password_old, business_password_new, business_password_en, props) => {
    const formData = new FormData()
    formData.append('business_id', business_id)
    formData.append('password', business_password_old)
    formData.append('business_password', business_password_new)
    formData.append('password_confim', business_password_en)

    return (dispatch) => {
        console.log(formData)
        let url = 'http://www.h5weixin.club:1024/api/business/3'
        fetch(url, {
            method: 'PUT',
            mode: 'cors',
            body: formData
        }).then((res) => {
            return res.json()
        }).then((res) => {
            errToast(res.msg)
            localStorage.setItem('user', JSON.stringify(res.data[0]))
            dispatch({
                type: 'ADD_UR',
                value: res.data[0]
            })
            props.history.goBack()
        }).catch((err) => {
            console.log(err);
        })
    }
}
const modifybusbind = (business_id, business_password_old, business_password_new, business_password_en, props) => {
    const formData = new FormData()
    formData.append('business_id', business_id)
    formData.append('password', business_password_old)
    formData.append('business_password', business_password_new)
    formData.append('password_confim', business_password_en)

    return (dispatch) => {
        console.log(formData)
        let url = 'http://www.h5weixin.club:1024/api/business/3'
        fetch(url, {
            method: 'PUT',
            mode: 'cors',
            body: formData
        }).then((res) => {
            return res.json()
        }).then((res) => {
            errToast(res.msg)
            localStorage.setItem('user', JSON.stringify(res.data[0]))
            dispatch({
                type: 'ADD_UR',
                value: res.data[0]
            })
            props.history.goBack()
        }).catch((err) => {
            console.log(err);
        })
    }
}
const modifybusdes = (business_id, business_describe, props) => {
    const formData = new FormData()
    formData.append('business_id', business_id)
    formData.append('business_describe', business_describe)

    return (dispatch) => {
        console.log(formData)
        let url = 'http://www.h5weixin.club:1024/api/business/3'
        fetch(url, {
            method: 'PUT',
            mode: 'cors',
            body: formData
        }).then((res) => {
            return res.json()
        }).then((res) => {
            errToast(res.msg)
            localStorage.setItem('user', JSON.stringify(res.data[0]))
            dispatch({
                type: 'ADD_UR',
                value: res.data[0]
            })
            props.history.goBack()
        }).catch((err) => {
            console.log(err);
        })
    }
}

export { modifybus, modifybussoft, modifybusbind, modifypaybus ,modifybusdes}